Python

What is Web Scraping using Python | part-1

What is Web Scraping using Python | part-1

What is Web Scraping using Python | part-1

Using any algorithm or programs to extract and process a large number of data from the Web is termed as WEB SCRAPING. Many programming languages can handle crawling and scrapping. Languages like Node.Js,

Ruby, PHP, C & C++ are the most popular programming languages for web scrapping. But if we talk about the best one then, python programming is the best one for web crawling. It can handle these processes smoothly.

If we talk about why Python is used for web scraping then the simplest answer will be the libraries like ‘Scrapy’ and ‘BeautifulSoup’. Both these libraries are very awesome and are designed for highly efficient and fast data extractions.

Is Web Scraping illegal?  

If we talk about the legality of web scraping and crawling, then let me clear you first, both of these are legal by themselves. We can scrap almost all Websites if HTML, CSS, Javascript, and contents are available and public.

How to check whether the website allows web scraping or not?

To check whether the website allows scraping and crawling either by python or any tool or other language we just need to search for the website's robots.txt file. 

Syntax: 

websitesname/robots.txt

What is the robot.txt file?

This is a file that is created by web designers to prevent the search engine and bots to scrape and crawl up their websites. This text file is searched by the bots to check whether the following website allows web scraping or not. It is needed and important if the designer wants to keep the entire section of the website private. This file is created on the notepad and is placed on the webserver. 

About Libraries: 

Python always comes with lots of handy and easily accessible libraries. Web Scraping is one of the interesting applications of Python. Python provides two libraries names ‘Scrapy’ and ‘BeaultifulSoup’. Both of these libraries are very awesome and designed for handling scrapping and crawling smoothly. So now let's discuss one by one about these libraries.

  1. Scrapy:

 It is a library that can be used for web scraping using python. This library is best for large scale webs crapping, It almost has all the functions and methods that allow efficient scraping and processing of data from websites. We can easily store it in any format as we want. 

  1. BeautifulSoup:  

It is also a library in python that allows scraping. But it is less efficient than Scrapy. It is a smaller package that will help us to get the website information out. It doesn’t help in downloading, cleaning, and saving the data. We can use any of the two libraries, but at the beginners' level, I would suggest starting with BeautifulSoup.

From the above discussion, the introduction of web-scraping must be clear so let's discuss the steps that we will follow for scraping the websites. So let's start discussing the steps:

Step0: Setting up the environment:

  • To use the power of python to scrap the websites, we don’t need to reinvent the wheel.
  • We will use existing libraries to get the job done.
  • We will install libraries.
  1. requests: Helps to get the content of the website from the internet.

Syntax of installation:

 pip install requests

Syntax of importing:

 import requests
  1. html5lib: It helps to parse and search.

Syntax of installation:

pip install html5lib

Syntax of importing:

import html5lib
  1. bs4: It uses the parser to provide different functions so that we can search and explore.

Syntax of installation:

pip install bs4

Syntax of importing:

from bs4 import BeautifulSoup

 

Step1: Fetching the HTML content:

  • To work with HTML, we will have to get HTML as a string.
  • We will leverage the power of the python request module to get this done.
  • The next step then will be to parse the HTML content and give it a tree-like structure So that it can be traversed.

Step2: Parse the HTML

  • Once the HTML is fetched using requests as a string, we need to parse it.
  • For parsing, we will use python’s BeautifilSoup which will create a tree-like structure for our DOM(Document Object Model.

Step3: Once the HTML is fetched:

Once the HTML is fetched and parsed, the next step is to manipulate

the tree using BeautifulSoup’s function to get our job done.

Through this article we discussed what is web-scraping, its legality, robot.txt file what are the modules provide by python to handle scrapping, and at last how to get started and traverse the tree. In my upcoming articles, we will have more discussion on web scraping. After learning all those you will be able to scrape any website and its contents into your desired format. 

So stay tuned with us, I hope you like this article.

For any issues feel free to comment and don’t forget to share the article with your friend.

Happy learning

Mousmi Kumari

Computer Science Engineer/Machine Learner/Python Developer